package com.example.controller;

import com.example.common.result.Result;
import com.example.entity.vo.DocVo;
import com.example.entity.vo.LotVo;
import com.example.entity.vo.Path;
import com.example.mapper.CmsmqMapper;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/api/Saas")
@Api(tags = "通用相关接口")
@Slf4j
public class SaasContrller {

    @Resource
    private CmsmqMapper cmsmqMapper;

    @ApiOperation("查询各个单别信息 doc")
    @GetMapping({"/listDoc/{doc}/docType"})
    public Result listDocdoc(@PathVariable(value = "doc",required = false) String doc,
                          @PathVariable(value = "docType",required = false) String docType){
        List<DocVo> docList = cmsmqMapper.listDoc(doc,docType);
        return Result.ok(docList);
    }

    @ApiOperation("查询各个单别信息 docType")
    @GetMapping({"/listDoc/doc/{docType}"})
    public Result listDocdocType(@PathVariable(value = "doc",required = false) String doc,
                          @PathVariable(value = "docType",required = false) String docType){
        List<DocVo> docList = cmsmqMapper.listDoc(doc,docType);
        return Result.ok(docList);
    }

    @ApiOperation("获取批号")
    @GetMapping("getLot")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo",value = "单号",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "stashNo",value = "仓库编号",dataType = "String",paramType = "query"),
    })
    public Result<List<LotVo>> getLot(@RequestParam(value = "orderNo") String orderNo,
                                      @RequestParam(value = "stashNo") String stashNo){
        List<LotVo> list = cmsmqMapper.getLot(orderNo,stashNo);
        if (list==null||list.size()==0){
            LotVo lotVo = new LotVo();
            lotVo.setLot("********************");
            list.add(lotVo);
            return Result.success(list,"已启用批号,没有批号,请检查品号档");
        }
        return Result.ok(list);
    }

    //出库生成批号用的
    @ApiOperation("是否启用批号")
    @GetMapping("isLot")
    public Result<LotVo> isLot(@RequestParam(value = "orderNo") String orderNo,
                               @RequestParam(value = "proDate",required = false) String proDate){
        String lot = "********************";
        String msg = "Y";
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = currentTime.format(yyyyMMdd);
        Map<String,Object> isLot = cmsmqMapper.isLot(orderNo);
        if (isLot==null){
            return Result.ok();
        }
        LotVo lotVo = new LotVo();
        if (!isLot.get("MB022").equals("N")){
            if (isLot.get("MB127").equals("Y") && (proDate==null||proDate.isEmpty())) {
                BigDecimal yesDate = (BigDecimal) isLot.get("MB023");
                LocalDateTime effectiveDate = currentTime.plusDays(yesDate.longValue());
                BigDecimal returnDate = (BigDecimal) isLot.get("MB024");
                LocalDateTime recheckDate = currentTime.plusDays(returnDate.longValue());
                lotVo.setStashNo((String) isLot.get("MB017"));
                lotVo.setProDate(format);
                lotVo.setEffectiveDate(effectiveDate.format(yyyyMMdd));
                lotVo.setRecheckDate(recheckDate.format(yyyyMMdd));
                lotVo.setLot(currentTime.format(yyyyMMdd));
            }else {
                if (proDate.isEmpty()) proDate = currentTime.format(yyyyMMdd);
                LocalDateTime currentDateTime = LocalDate.parse(proDate, yyyyMMdd).atStartOfDay();
                BigDecimal yesDate = (BigDecimal) isLot.get("MB023");
                LocalDateTime localDateTime = currentDateTime.plusDays(yesDate.longValue());
                BigDecimal returnDate = (BigDecimal) isLot.get("MB024");
                LocalDateTime recheckDate = currentDateTime.plusDays(returnDate.longValue());
                lotVo.setStashNo((String) isLot.get("MB017"));
                lotVo.setEffectiveDate(localDateTime.format(yyyyMMdd));
                lotVo.setRecheckDate(recheckDate.format(yyyyMMdd));
                lotVo.setProDate(proDate);
                lotVo.setLot(currentTime.format(yyyyMMdd));
            }
            lotVo.setLot(currentTime.format(yyyyMMdd));
        }else {
            lotVo.setLot("********************");
            msg = "N";
        }
        return Result.success(lotVo,msg);
    }
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @GetMapping("/queryPath")
    @ApiOperation("查询所有请求路径")
    public Result<List<Path>> queryPath(){
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        List<Path> pathList = new ArrayList<>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
            RequestMappingInfo mappingInfo = entry.getKey();
            HandlerMethod handlerMethod = entry.getValue();

            Set<String> patterns = mappingInfo.getPatternsCondition().getPatterns();
            for (String pattern : patterns) {
                ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
                String operationDescription = apiOperation != null ? apiOperation.value() : "No description";

//                System.out.println("Path: " + pattern + " -> Method: " + handlerMethod.getMethod().getName() + " -> Description: " + operationDescription);
                Path path = new Path();
                path.setName(operationDescription);
                path.setPath(pattern);
                pathList.add(path);
            }
        }
        return Result.success(pathList);
    }
}
